package com.eversec.util.ftp;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import com.thoughtworks.xstream.XStream;

public class FileUploadXmlOper {
	
	public static XStream xs = new XStream();
	
	/**
	 * 按照规定的xml格式，创建文件上传xml文件,存储为文件
	 */
	public static String createFileUploadFileXml(String idcId, String dataUpload, int encryptAlgorithm, int compressionFormat, int hashAlgorithm, String dataHash){
		FileUploadXml fileXml = new FileUploadXml();
		fileXml.setIdcId(idcId);
		fileXml.setDataUpload(dataUpload);
		fileXml.setEncryptAlgorithm(encryptAlgorithm);
		fileXml.setCompressionFormat(compressionFormat);
		fileXml.setHashAlgorithm(hashAlgorithm);
		if(dataHash != null && dataHash.length() < 65){
			fileXml.setDataHash(dataHash);
		}else{
			fileXml.setDataHash("");
		}
		
		xs.processAnnotations(FileUploadXml.class);
		String fileXmlString = xs.toXML(fileXml);
		return fileXmlString;
	}
	
	/**
	 * 解析xml文件 
	 */
	public static FileUploadXml parseFileUploadXmlFromString(String xmlString){
		XStream xs = new XStream();
		xs.processAnnotations(FileUploadXml.class);
		InputStream myIn=new ByteArrayInputStream(xmlString.getBytes());
		return (FileUploadXml) xs.fromXML(myIn);
	}
}
